package com.wsl.Controller;

import com.lww.springcloudapi.pojo.*;
import com.lww.springcloudapi.result.LayuiEntity;
import com.lww.springcloudapi.vo.Kh;
import com.wsl.service.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Controller
@CrossOrigin
public class AddUserController {
    @Autowired
    private AddUserService addUserService;
    @Autowired
    private AddUserMessageService addUserMessageService;
    @Autowired
    private AddUserPakageService addUserPakageService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private HmService hmService;
    @ApiOperation(value = "查看全部号码")
    @RequestMapping("/xh")
    @ResponseBody
    public LayuiEntity selectSetMeal() {
        //1.创建返回对象
        LayuiEntity layuiEntity = new LayuiEntity();
        try {
            List<HM> list;
            //2.包装返回对象
             list = hmService.selectt();
            layuiEntity.setData(list);
            layuiEntity.setCode("200");
            layuiEntity.setMsg("查询成功");
        } catch (Exception e) {
            layuiEntity.setCode("500");
            layuiEntity.setMsg("查询失败，请联系管理员");
        }
        return layuiEntity;
    }

    @RequestMapping("/OpenAnAccount")
    @ResponseBody
    @Transactional
    public String toLogin(Kh kh) throws ParseException {
        Date date=new Date();
        System.out.println(date);
        kh.setRegistrationtime(date);
        kh.setBusinesshall("38");
        kh.setUserpassword("EFG@AB");
        //给用户表添加信息
        TabHallUser tabHallUser=new TabHallUser();
        tabHallUser.setPnumber(kh.getPnumber());
        tabHallUser.setUserpassword(kh.getUserpassword());
        addUserService.AddUser(tabHallUser);

        //xiugai
        hmService.xg(kh.getPnumber());

        //给手机信息表添加信息
        TabHallUsermessage tabHallUsermessage=new TabHallUsermessage();
        tabHallUsermessage.setSetmealid(kh.getSetmealid());
        tabHallUsermessage.setPnumber(kh.getPnumber());
        tabHallUsermessage.setIdname(kh.getIdname());
        tabHallUsermessage.setIdentity(kh.getIdentity());
        tabHallUsermessage.setRegistrationtime(kh.getRegistrationtime());
        tabHallUsermessage.setBusinesshall(kh.getBusinesshall());
        addUserMessageService.AddUserMessage(tabHallUsermessage);

        //查出套餐信息
        TabHallSetmeal select = setmealService.select(kh.getSetmealid());

        //添加套餐变量，初始化用户套餐
        TabHallUserpackage tabHallUserpackage=new TabHallUserpackage();
        tabHallUserpackage.setPnumber(kh.getPnumber());
        tabHallUserpackage.setFlow(select.getFlow());
        tabHallUserpackage.setConversationtime(select.getConversationtime());
        tabHallUserpackage.setMessage(select.getMessage());
        tabHallUserpackage.setMoney(100-select.getMoney());
        tabHallUserpackage.setSetmealid(kh.getSetmealid());
        addUserPakageService.AddUserPakage(tabHallUserpackage);
        return "成功！";
    }

}
